package com.joyque.androidbug.report.service.impl;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.joyque.androidbug.report.dao.ApplicationRepository;
import com.joyque.androidbug.report.dao.BugRepository;
import com.joyque.androidbug.report.entity.Application;
import com.joyque.androidbug.report.entity.Bug;
import com.joyque.androidbug.report.service.ApplicationService;
import com.joyque.androidbug.report.service.BugService;

@Service
@Transactional
public class ApplicationServiceImpl implements ApplicationService{

	@Inject
	private ApplicationRepository applicationRepository;
	
	@Inject
	private BugService bugService;
	
	@Override
	public Application saveApplication(String packageName, String content, String device_id, String system_version, String code_version, String pId) {
		Application app = applicationRepository.findByName(packageName);
		 if(app == null){
			app = new Application();
			app.setName(packageName);
			app.setStatus(Application.STATUS.NORMAL.ordinal());
			app.setCreated_at(new Date());
			Application a = applicationRepository.saveAndFlush(app);
			bugService.saveBug(content, device_id, system_version, a, code_version, pId);
		}else if(app.getStatus() != Application.STATUS.CLOSE.ordinal()){
			bugService.saveBug(content, device_id, system_version, app, code_version, pId);
		}
		return app;
	}

	


	@Override
	public List<Application> findAll() {
		return applicationRepository.findAll();
	}


	@Override
	public List<Application> searchApplication() {
		return null;
	}

}
